package cn.jdy.plugins.wap;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.platform.service.ConfService;
import cn.jdy.plugins.entity.Mall;
import cn.jdy.plugins.entity.Productx;
import cn.jdy.plugins.service.MallService;
import cn.jdy.plugins.service.ProductxService;

/**
 * @title 商家管理前台操作
 * @author jga
 * @since 3-12-2015
 */

@Controller
@RequestMapping("/mall")
public class MallCtrl extends BaseCtrl{
	@Autowired
	private MallService MallService;
	@Autowired
	private ConfService confService;

  @Autowired
  private ProductxService productxService;
	/**
	 *  商家列表
	 */
  @RequestMapping("")
	public ModelAndView list(Mall entity, String fromDate, String toDate) {
	  ModelAndView mv = new ModelAndView("/mall/mallList.jsp");
    entity.setStatus("1");
	  mv.addObject("dataList", MallService.querypage(entity));
	  mv.addObject("iTotalDisplayRecords", MallService.count(entity));
		mv.addObject("entity", entity);
		return mv;
	}
	
	/**
	 * 异步查询
	 * @param sp
	 * @param fromDate
	 * @param toDate
	 * @return
	 */
	@RequestMapping("/queryByPage")
	@ResponseBody
	public Map<String, Object> queryByPage(Mall sp){
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
		  sp.setStatus("1");
			jsonMap.put("dataList", MallService.querypage(sp));
			jsonMap.put("iTotalDisplayRecords", MallService.count(sp));
			jsonMap.put("iTotalRecords", MallService.count());
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "查询异常", e);
		}
		return jsonMap;
	}
	
	/**
	 * 新增
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit() {
		return edit(null);
	}
	
	/**
	 * 编辑
	 * @param id
	 * @return
	 */
	@RequestMapping("/show-{id}.html")
	public ModelAndView edit(@PathVariable Integer id) {
		ModelAndView mv = new ModelAndView("/mall/mallDetail.jsp");
		mv.addObject("data", id==null||id==0 ? id : this.MallService.find(id));
		 if(id!=null){
		   Productx px=new Productx();
		 //  Mall m=new Mall();
		//   m.setId(id);
		 //  px.setMall(m);
       DetachedCriteria dc = DetachedCriteria.forClass(px.getClass());
       dc.add(Restrictions.eq("mallId", id));
  //     dc.addOrder(Order.desc("createTime"));
		   List<Productx> list=   productxService.find(dc,0,4);
		   mv.addObject("plist",list);
		 }
		return mv;
	}
	
	 @RequestMapping("/showadd-{id}.html")
	  public ModelAndView queryadd(@PathVariable Integer id) {
	    ModelAndView mv = new ModelAndView("/productx/mallAddress.jsp");
	    mv.addObject("dataadd", id==null||id==0 ? id : this.MallService.find(id));
	    return mv;
	  }
	
	
}
